package com.carbonspark.ecoquest.application.controller;

import com.alibaba.fastjson.JSON;
import com.carbonspark.auth.api.UserFeignService;


import com.carbonspark.auth.entity.AuthUserDTO;
import com.carbonspark.ecoquest.application.convert.TaskUserDTOConverter;

import com.carbonspark.ecoquest.application.dto.TaskDTO;
import com.carbonspark.ecoquest.application.dto.TaskUserDTO;

import com.carbonspark.auth.entity.Result;
import com.carbonspark.ecoquest.common.utils.LoginUtil;

import com.carbonspark.ecoquest.domain.entity.TaskUserBO;
import com.carbonspark.ecoquest.domain.service.TaskDomainService;
import com.carbonspark.ecoquest.domain.service.TaskUserDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 任务与用户关联接口
 */

@RestController
@RequestMapping("/taskUser")
@Slf4j
public class TaskUserController {
    @Resource
    private TaskUserDomainService taskUserDomainService;
    @Resource
    private UserFeignService userFeignService;

    /**
     * 添加任务与用户关系
     * @param taskUserDTO
     * @return
     */
   @PostMapping("add")
    public Result<TaskDTO> add(@RequestBody TaskUserDTO taskUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TaskUserController.add.dto:{}", JSON.toJSONString(taskUserDTO));
            }
            Preconditions.checkNotNull(taskUserDTO.getTaskId(),"任务id不能为空");
            String loginId = LoginUtil.getLoginId();
            AuthUserDTO userDTO = new AuthUserDTO();
            userDTO.setUserName(loginId);
          Result<AuthUserDTO> authUserDTOResult = userFeignService.getUserInfo(userDTO);
          //获取当前用户的id
            Long id = authUserDTOResult.getData().getId();
           Boolean result=  taskUserDomainService.insertTaskUser(id,taskUserDTO.getTaskId());


            return Result.ok(result);
        } catch (Exception e) {
            log.error("TaskUserController.add.error:{}", e.getMessage(), e);
            return Result.fail("添加任务失败");
        }
    }

    /**
     * 放弃任务
     * @param taskUserDTO
     * @return
     */
    @PostMapping("abandon")
    public Result<TaskDTO> abandon(@RequestBody TaskUserDTO taskUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TaskUserController.abandon.dto:{}", JSON.toJSONString(taskUserDTO));
            }
            Preconditions.checkNotNull(taskUserDTO.getTaskId(),"任务id不能为空");
            String loginId = LoginUtil.getLoginId();
            AuthUserDTO userDTO = new AuthUserDTO();
            userDTO.setUserName(loginId);
            Result<AuthUserDTO> authUserDTOResult = userFeignService.getUserInfo(userDTO);
            //获取当前用户的id
            Long id = authUserDTOResult.getData().getId();
            Boolean result=  taskUserDomainService.delete(id,taskUserDTO.getTaskId());


            return Result.ok(result);
        } catch (Exception e) {
            log.error("TaskUserController.abandon.error:{}", e.getMessage(), e);
            return Result.fail("放弃任务失败");
        }
    }    /**
     * 添加任务与用户关系
     * @param taskUserDTO
     * @return
     */
    @PostMapping("isTask")
    public Result<TaskDTO> isTask(@RequestBody TaskUserDTO taskUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TaskUserController.isTask.dto:{}", JSON.toJSONString(taskUserDTO));
            }
            Preconditions.checkNotNull(taskUserDTO.getTaskId(),"任务id不能为空");
            String loginId = LoginUtil.getLoginId();
            AuthUserDTO userDTO = new AuthUserDTO();
            userDTO.setUserName(loginId);
            Result<AuthUserDTO> authUserDTOResult = userFeignService.getUserInfo(userDTO);
            //获取当前用户的id
            Long id = authUserDTOResult.getData().getId();
            Boolean result=  taskUserDomainService.queryByTask(id,taskUserDTO.getTaskId());


            return Result.ok(result);
        } catch (Exception e) {
            log.error("TaskUserController.isTask.error:{}", e.getMessage(), e);
            return Result.fail("查询是否挑战任务失败");
        }
    }

    /**
     * 查询坚持多少天
     * @param taskUserDTO
     * @return
     */
    @PostMapping("queryDays")
    public Result<TaskDTO> queryDays(@RequestBody TaskUserDTO taskUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TaskUserController.queryDays.dto:{}", JSON.toJSONString(taskUserDTO));
            }
            Preconditions.checkNotNull(taskUserDTO.getTaskId(),"任务id不能为空");
            String loginId = LoginUtil.getLoginId();
            AuthUserDTO userDTO = new AuthUserDTO();
            userDTO.setUserName(loginId);
            Result<AuthUserDTO> authUserDTOResult = userFeignService.getUserInfo(userDTO);
            //获取当前用户的id
            Long id = authUserDTOResult.getData().getId();
            TaskUserBO taskUserBO=  taskUserDomainService.queryDays(id,taskUserDTO.getTaskId());
            TaskUserDTO tudto = TaskUserDTOConverter.INSTANCE.convertBOToDTO(taskUserBO);

            return Result.ok(tudto);
        } catch (Exception e) {
            log.error("TaskUserController.queryDays.error:{}", e.getMessage(), e);
            return Result.fail("查询坚持多少天失败");
        }
    }
    /**
     * 打卡
     * @param taskUserDTO
     * @return
     */
    @PostMapping("clock")
    public Result<TaskDTO> clock(@RequestBody TaskUserDTO taskUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TaskUserController.clock.dto:{}", JSON.toJSONString(taskUserDTO));
            }
            Preconditions.checkNotNull(taskUserDTO.getTaskId(),"任务id不能为空");
            String loginId = LoginUtil.getLoginId();
            AuthUserDTO userDTO = new AuthUserDTO();
            userDTO.setUserName(loginId);
            Result<AuthUserDTO> authUserDTOResult = userFeignService.getUserInfo(userDTO);
            //获取当前用户的id
            Long id = authUserDTOResult.getData().getId();
            Boolean result=  taskUserDomainService.clock(id,taskUserDTO.getTaskId());


            return Result.ok(result);
        } catch (Exception e) {
            log.error("TaskUserController.clock.error:{}", e.getMessage(), e);
            return Result.fail("打卡失败失败");
        }
    }

}
